using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VMSKLDEF
    {
        public DataRow Convert2MSKLDEF(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryMSKLDEF(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["MKDMANDANT"] = " ".PadRight(2 ); 
         dwReturn["MKDEKL"] = " ".PadRight(7 ); 
         dwReturn["MKDSTAKZ"] = " ".PadRight(1 ); 
         dwReturn["MKDBEZ"] = " ".PadRight(40 ); 
         dwReturn["MKDSCHLAGW1"] = " ".PadRight(20 ); 
         dwReturn["MKDSCHLAGW2"] = " ".PadRight(20 ); 
         dwReturn["MKDSCHLAGW3"] = " ".PadRight(20 ); 
         dwReturn["MKDSCHLAGW4"] = " ".PadRight(20 ); 
         dwReturn["MKDSCHLAGW5"] = " ".PadRight(20 ); 
         dwReturn["MKDSCHLAGW6"] = " ".PadRight(20 ); 
         dwReturn["MKDFDFELD1"] = " ".PadRight(41 ); 
         dwReturn["MKDFDFELD2"] = " ".PadRight(41 ); 
         dwReturn["MKDFDFELD3"] = " ".PadRight(41 ); 
         dwReturn["MKDFDFELD4"] = " ".PadRight(41 ); 
         dwReturn["MKDFDFELD5"] = " ".PadRight(41 ); 
         dwReturn["MKDFDFELD6"] = " ".PadRight(41 ); 
         dwReturn["MKDFDFELD7"] = " ".PadRight(41 ); 
         dwReturn["MKDFDFELD8"] = " ".PadRight(41 ); 
         dwReturn["MKFILLER"] = " ".PadRight(147 ); 
         dwReturn["MKDAENDZL"] = " ".PadRight(3 ); 
         dwReturn["MKDAENDBEN"] = " ".PadRight(8 ); 
         dwReturn["MKDAENDDAT"] = " ".PadRight(6 ); 
         dwReturn["MKDNEUBEN"] = " ".PadRight(8 ); 
         dwReturn["MKDNEUDAT"] = " ".PadRight(6 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapMSKLDEF(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveMSKLDEF(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryMSKLDEF(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapMSKLDEF(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertMSKLDEF(dwLocal, ITXDBVERSION);
            }
            UpdateMSKLDEF(dwLocal);
        }
        public void InsertMSKLDEF(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO MSKLDEF(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryMSKLDEF(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM MSKLDEF WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM MSKLDEF  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "MSKLDEF";
            Return = Result;
            return Return;
        }
        public void DeleteMSKLDEF(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM MSKLDEF   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateMSKLDEF(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE MSKLDEF SET "
                    +"MKDMANDANT= :MKDMANDANT,"
                    +"MKDEKL= :MKDEKL,"
                    +"MKDSTAKZ= :MKDSTAKZ,"
                    +"MKDBEZ= :MKDBEZ,"
                    +"MKDSCHLAGW1= :MKDSCHLAGW1,"
                    +"MKDSCHLAGW2= :MKDSCHLAGW2,"
                    +"MKDSCHLAGW3= :MKDSCHLAGW3,"
                    +"MKDSCHLAGW4= :MKDSCHLAGW4,"
                    +"MKDSCHLAGW5= :MKDSCHLAGW5,"
                    +"MKDSCHLAGW6= :MKDSCHLAGW6,"
                    +"MKDFDFELD1= :MKDFDFELD1,"
                    +"MKDFDFELD2= :MKDFDFELD2,"
                    +"MKDFDFELD3= :MKDFDFELD3,"
                    +"MKDFDFELD4= :MKDFDFELD4,"
                    +"MKDFDFELD5= :MKDFDFELD5,"
                    +"MKDFDFELD6= :MKDFDFELD6,"
                    +"MKDFDFELD7= :MKDFDFELD7,"
                    +"MKDFDFELD8= :MKDFDFELD8,"
                    +"MKFILLER= :MKFILLER,"
                    +"MKDAENDZL= :MKDAENDZL,"
                    +"MKDAENDBEN= :MKDAENDBEN,"
                    +"MKDAENDDAT= :MKDAENDDAT,"
                    +"MKDNEUBEN= :MKDNEUBEN,"
                    +"MKDNEUDAT= :MKDNEUDAT,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("MKDMANDANT", OracleType.NVarChar).Value = dwData["MKDMANDANT"].ToString(); 
          or1.Parameters.Add("MKDEKL", OracleType.NVarChar).Value = dwData["MKDEKL"].ToString(); 
          or1.Parameters.Add("MKDSTAKZ", OracleType.NVarChar).Value = dwData["MKDSTAKZ"].ToString(); 
          or1.Parameters.Add("MKDBEZ", OracleType.NVarChar).Value = dwData["MKDBEZ"].ToString(); 
          or1.Parameters.Add("MKDSCHLAGW1", OracleType.NVarChar).Value = dwData["MKDSCHLAGW1"].ToString(); 
          or1.Parameters.Add("MKDSCHLAGW2", OracleType.NVarChar).Value = dwData["MKDSCHLAGW2"].ToString(); 
          or1.Parameters.Add("MKDSCHLAGW3", OracleType.NVarChar).Value = dwData["MKDSCHLAGW3"].ToString(); 
          or1.Parameters.Add("MKDSCHLAGW4", OracleType.NVarChar).Value = dwData["MKDSCHLAGW4"].ToString(); 
          or1.Parameters.Add("MKDSCHLAGW5", OracleType.NVarChar).Value = dwData["MKDSCHLAGW5"].ToString(); 
          or1.Parameters.Add("MKDSCHLAGW6", OracleType.NVarChar).Value = dwData["MKDSCHLAGW6"].ToString(); 
          or1.Parameters.Add("MKDFDFELD1", OracleType.NVarChar).Value = dwData["MKDFDFELD1"].ToString(); 
          or1.Parameters.Add("MKDFDFELD2", OracleType.NVarChar).Value = dwData["MKDFDFELD2"].ToString(); 
          or1.Parameters.Add("MKDFDFELD3", OracleType.NVarChar).Value = dwData["MKDFDFELD3"].ToString(); 
          or1.Parameters.Add("MKDFDFELD4", OracleType.NVarChar).Value = dwData["MKDFDFELD4"].ToString(); 
          or1.Parameters.Add("MKDFDFELD5", OracleType.NVarChar).Value = dwData["MKDFDFELD5"].ToString(); 
          or1.Parameters.Add("MKDFDFELD6", OracleType.NVarChar).Value = dwData["MKDFDFELD6"].ToString(); 
          or1.Parameters.Add("MKDFDFELD7", OracleType.NVarChar).Value = dwData["MKDFDFELD7"].ToString(); 
          or1.Parameters.Add("MKDFDFELD8", OracleType.NVarChar).Value = dwData["MKDFDFELD8"].ToString(); 
          or1.Parameters.Add("MKFILLER", OracleType.NVarChar).Value = dwData["MKFILLER"].ToString(); 
          or1.Parameters.Add("MKDAENDZL", OracleType.NVarChar).Value = dwData["MKDAENDZL"].ToString(); 
          or1.Parameters.Add("MKDAENDBEN", OracleType.NVarChar).Value = dwData["MKDAENDBEN"].ToString(); 
          or1.Parameters.Add("MKDAENDDAT", OracleType.NVarChar).Value = dwData["MKDAENDDAT"].ToString(); 
          or1.Parameters.Add("MKDNEUBEN", OracleType.NVarChar).Value = dwData["MKDNEUBEN"].ToString(); 
          or1.Parameters.Add("MKDNEUDAT", OracleType.NVarChar).Value = dwData["MKDNEUDAT"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
